package com.zh.system.controller;

import com.zh.common.annotation.Operator;
import com.zh.web.domain.system.dto.DeleteFileDto;
import com.zh.system.domain.dto.MergeChunkDto;
import com.zh.system.domain.dto.UploadChunkDto;
import com.zh.system.domain.dto.UploadFileDto;
import com.zh.system.service.FileService;
import com.zh.web.base.ApiResult;
import com.zh.web.base.BaseController;
import io.minio.errors.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Valid
@Slf4j
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
@Api(tags = "系统服务-文件接口", value = "文件管理")
@Operator
public class FIleController extends BaseController {
    private final FileService fileService;

    @PostMapping("/upload")
    @ApiOperation(value = "小文件直传", notes = "参数名为file,批量前端做")
    public ApiResult<String> uploadFile(@RequestParam("fileName") String fileName,
                                        @RequestPart("file") MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        UploadFileDto uploadFileDto = new UploadFileDto();
        uploadFileDto.setCreateOperator(operator);
        uploadFileDto.setUpdateOperator(operator);
        uploadFileDto.setFileName(fileName);
        uploadFileDto.setFile(file);
        return buildSuccessPackage(fileService.saveFile(uploadFileDto));
    }

    @PostMapping("/chunk/upload")
    @ApiOperation("上传分片,文件/分片已上传=>立即返回（该返回为分片md5无实际意义） 视为断点续传")
    public ApiResult<String> uploadChunk(@RequestParam("fileName") String fileName,
                                         @RequestParam("fileMd5") String fileMd5,
                                         @RequestParam("chunkMd5") String chunkMd5,
                                         @RequestParam("chunk") MultipartFile chunk) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        UploadChunkDto uploadChunkDto = UploadChunkDto.builder()
                .fileName(fileName)
                .fileMd5(fileMd5)
                .chunkMd5(chunkMd5)
                .chunk(chunk).build();
        return buildSuccessPackage(fileService.saveChunk(uploadChunkDto));
    }

    @PostMapping("/chunk/merge")
    @ApiOperation("合并分片，文件已上传=>立即返回url 视为秒传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", required = true, dataType = "string", paramType = "header")
    })
    public ApiResult<String> mergeChunk(@RequestBody MergeChunkDto mergeChunkDto) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        mergeChunkDto.setCreateOperator(operator);
        mergeChunkDto.setUpdateOperator(operator);
        return buildSuccessPackage(fileService.mergeChunk(mergeChunkDto));
    }

    @DeleteMapping
    @ApiOperation(value = "通过url删除文件")
    public ApiResult<String> deleteFileByUrl(@RequestBody DeleteFileDto deleteFileDto) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        deleteFileDto.setUpdateOperator(operator);
        return buildMsgPackage(fileService.deleteFileByUrl(deleteFileDto));
    }
}
